iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
自我挑戰組

CPE題目練習系列 第 8

[Day8]Rare Easy Problem

  • 分享至 

  • xImage
  •  

上一篇介紹了2 the 9s,是一題會需要重複執行的題目,使用迴圈跟副程式會比較容易執行,整體上不會太難的一題。

今天要來講解的題目是 Rare Easy Problem,
附上程式碼,

import java.util.;
import static java.lang.System.
;
public class main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
long a=sc.nextLong();
boolean first =true;
if(a==0) break;

	for(int i =9;i>=0;i--){
		if((a-i)%9==0){
			if(first==false) System.out.print(" ");
			System.out.print((a-i)/9*10+i);
			first = false;
		}
		
	}
 System.out.println();

}

}
};

題目要求輸入一個數字,那個數字至少二位數,那個數字是N-M得來的,M是N除以10的整數(%10),要我們輸出可能的N,並由小排到大,
算式是這樣
N = 10*M + i ( 0 ≦ i ≦ 9)
N - M = 9M + i
N - M - i = 9M
則N = [N - M - i ] / 9 * 10 + i
( 9M )
只要推倒出公式的話這題就會很簡單了,接下來就是使用迴圈判斷i是多少,i越大N越小,所以從9到0,才會從小排到大,因為不只一個,所以要寫一個判斷空格的才能跟題目要求的輸出一模一樣。
今天就講解到這裡,這題我覺得算是比較難一點的題目,但是也不到二星那麼難,主要題目是在考你的數學能力,然後再看你能不能想辦法寫出你腦中的想法。
明天也是新的題目,會介紹Beat the Spread!,繼續加油!


上一篇
[Day7]2 the 9s
下一篇
[Day9]Beat the Spread!
系列文
CPE題目練習30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言